package com.example.oauth.authserver.model;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author chang.zhou
 * @version 1$
 * @date Created in 2022/3/6 0:22
 */
@Data
public class SubjectBO implements UserDetails {

    private String loginName;

    private String password;

    private Boolean accountNotExpired;

    private Boolean accountNotLocked;

    private Boolean credentialsNotExpired;

    private Boolean enabled;

    public SubjectBO(String loginName, String password, Boolean accountNotExpired, Boolean accountNotLocked, Boolean credentialsNotExpired, Boolean enabled) {
        this.loginName = loginName;
        this.password = password;
        this.accountNotExpired = accountNotExpired;
        this.accountNotLocked = accountNotLocked;
        this.credentialsNotExpired = credentialsNotExpired;
        this.enabled = enabled;
    }

    private List<String> roles = new ArrayList<>(1);

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return AuthorityUtils.createAuthorityList(roles.toArray(new String[]{}));
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.loginName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.accountNotExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.accountNotLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.credentialsNotExpired;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}
